package com.supermap.dataHandling.service;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.springframework.stereotype.Service;

/**
 * @author bruce
 * @date 18/8/23 10:26
 * @Description: 描述--etl调用的服务类
 */
@Service
public class EtlService {
    public static void main(String[] args) throws KettleException {
//        runTrans("D:\\test.ktr");
//        runJob("F:\\zhgl\\zhibao\\test_job.kjb");
    }

    public static Boolean runJob(String pathjob, String reportDate) throws KettleException {
        KettleEnvironment.init(); //初始化
        JobMeta jobMeta = new JobMeta(pathjob, null);
        Job job = new Job(null, jobMeta);
        job.setVariable("job_date", reportDate);
        job.start();
        job.waitUntilFinished();
        if (job.getErrors() > 0) {
            System.out.println("作业过程中发生异常");
            return false;
        }
        return true;
    }

//    // 调用Transformation示例：
//    public static void runTrans(String filename) {
//        try {
//            KettleEnvironment.init();
//            TransMeta transMeta = new TransMeta(filename);
//            Trans trans = new Trans(transMeta);
//            //设置参数
//            trans.setParameterValue("file", "D:\\b.xls");
//            trans.prepareExecution(null);
//            trans.startThreads();
//            trans.waitUntilFinished();
//            if (trans.getErrors() != 0) {
//                System.out.println("Error");
//            } else {
//                System.out.println("success");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
